var api = require('../../utils/api.js')
var app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        name:'',
        pass:'',
        unionid:'',
        openid:''
    },
    onShow(){
         //调用登录接口
         var that=this;
         wx.login({
             success: function (ret) {
             console.log(ret);
             wx.getUserInfo({
                 success: function () {
                     api.post({
                         url: 'wxapp/getopenid',
                         data: {'code':ret.code},
                         success: data => {
                             that.setData({
                                 openid:data.data.openid,
                                 unionid:data.data.unionid
                             })
                         }
                     })
                 }
             })
 
             that.setData({
                 code:ret.code
             })
             }
         })
    },

    showtips(){
        wx.showToast({
          title: '请联系管理员！',
          icon:'none'
        })
    },

    submits(){
        var name=this.data.name;
        var unionid=this.data.unionid;
        var openid=this.data.openid;

        var password=this.data.pass;
    

 
        api.post({
            url: 'user/login',
            data: {'account':name,password:password,openid:openid,unionid:unionid},
            success: data => {
                var info=data.data;
                console.log(info) 
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
                if(data.code==1){
                    wx.setStorageSync('user', info.userinfo)
                    wx.setStorageSync('token', info.userinfo.token)
    
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                }
            }
        })
    },

    getname(ret){
        console.log(ret);
        this.setData({
            name:ret.detail.value
        })
    },
    getpass(ret){
        console.log(ret);
        this.setData({
            pass:ret.detail.value
        })
    }
    
})